#include "eventRule.h"
#include "utils.h"

using namespace bd;

EventRule::EventRule(Module* parent):Rule(parent)
{
	
}

EventRule::~EventRule()
{
	
}

bool EventRule::invoke(PageContext *context, Event &eventType)
{
    return true;
}

EventType  EventRule::getEventType() 
{
	return this->_type;
}

EventAble::EventAble() :_ruleSorted(false) 
{

}

EventAble::~EventAble() 
{
	
	std::list<Rule*>::iterator iter = _ruleList.begin();
	while(iter != _ruleList.end()) 
	{
		delete *iter;
		*iter = NULL;
		_ruleList.erase(iter++); 
	}
}

void EventAble::addEventRule(EventRule *rule) 
{
	std::list<EventRule*>::iterator it = this->_eventRuleList.begin();
	for(;it!=this->_eventRuleList.end();it++)
	{
		if((*it)->getOrder()>=rule->getOrder())
			break;
	}
	if(it!=this->_eventRuleList.end())
		this->_eventRuleList.insert(it,rule);
	else
		this->_eventRuleList.push_back(rule);
	this->_ruleSorted = true;
}

void EventAble::sortRules() 
{
	RuleUtils::sort(this->_eventRuleList);
	RuleUtils::sort(this->_ruleList);
	this->_ruleSorted = true;
}

std::list<EventRule*>* EventAble::getEventRules() 
{
	return &this->_eventRuleList;
}